package com.wyzz.hr.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.wyzz.hr.base.ActionAuths;
import com.wyzz.hr.base.UsersAuthority;
import com.wyzz.hr.bo.IHasAuthBO;
import com.wyzz.hr.spring.SpringBeanFactory;

import java.util.Iterator;
import java.util.List;

public class CheckAuth implements Interceptor {

	public CheckAuth() {
	}

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation actionInvocation) throws Exception {
		List authorityList = ActionAuths.getActionAuths(actionInvocation
				.getProxy().getActionName(), actionInvocation.getProxy()
				.getMethod());//找出所有具有执行action中此方法的权限。
		System.out.println(actionInvocation.getProxy().getActionName()
				+ "...................."
				+ actionInvocation.getProxy().getMethod());
		if (authorityList == null || authorityList.size() == 0) {
			actionInvocation.getStack().setValue("authorityCondition", null);
			return actionInvocation.invoke();
		}
		for (Iterator iterator = authorityList.iterator(); iterator.hasNext();) {
			UsersAuthority userAuthority = (UsersAuthority) iterator.next();
			if (hasAuthBo.hasActionAuth(userAuthority)) {
				actionInvocation.getStack().setValue("authorityCondition",
						userAuthority.getFilter());
				return actionInvocation.invoke();
			}
		}
		return "noauth";
	}

	private static final long serialVersionUID = 1L;
	private static final String NOAUTH = "noauth";
	private static IHasAuthBO hasAuthBo = (IHasAuthBO) SpringBeanFactory
			.getBean("hasService");

}
